package com.nghiatran.utils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.nghiatran.common.Constants;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

 /**
 * @author TRAN NGHIA 
 * @Email  tranducnghiabkhn@gmail.com
 */


public class Utils implements Constants  {

    private static final String TAG = Utils.class.getName();

    public static boolean checkValidString(String str) {
        if (str == null) {
            return false;
        }

        str = str.trim();
        if (str.equals("")) {
            return false;
        }

        return true;
    }

    /**
     * 
     * @param email
     *            address
     * @return True if valid and False if not valid
     */
    public static boolean checkValidEmail(String email) {
        if (email == null) {
            return false;
        }

        String inputStr = email.trim();
        Pattern pattern = Pattern.compile(EMAIL_REGEX, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(inputStr);

        if (matcher.matches()) {
            return true;
        }
        return false;
    }

    public static boolean checkNetwork(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = cm.getActiveNetworkInfo();
        if (networkInfo != null) {
            if (networkInfo.isConnected()) {
                return true;
            }
        }
        return false;
    }

    public static String inputStreamToString(InputStream is) {
        String line = "";
        StringBuilder total = new StringBuilder();
        // Wrap a BufferedReader around the InputStream
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        // Read response until the end
        try {
            while ((line = rd.readLine()) != null) {
                total.append(line);
            }
        } catch (IOException e) {
            Log.e(TAG, "Error read data");
            return null;
        }
        // Return full string
        return total.toString();
    }

    public static String formatDate(String dateStr, String oldPattern, String newPattern) {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat(oldPattern);
        try {
            date = sdf.parse(dateStr);
        } catch (ParseException e) {
            Log.e("Date Parse", "Error parse date: " + e.getMessage());
            return null;
        }

        sdf.applyPattern(newPattern);
        return sdf.format(date);
    }


}
